<?php
class Products_IndexController extends Zendme_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 8,
									'pageRange' => 4,
								  ); 
	protected $_namespace;
	
	public function init(){
		
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		
		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] 
									 . '/' . $this->_arrParam['controller'];		
		//Duong dan cua Action chinh		
		$this->_actionMain = '/' . $this->_arrParam['module'] 
							 . '/' . $this->_arrParam['controller']	. '/index';
							 
		$this->_paginator['currentPage'] = $this->_request->getParam('page',1);
		$this->_arrParam['paginator'] = $this->_paginator;
		
		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		//$ssFilter->unsetAll();
		if(empty($ssFilter->col)){
			$ssFilter->lang 		= '';
		}
		$this->_arrParam['ssFilter']['lang'] 		= $this->_arrParam['lang'];	
		
		$info = new Zendme_System_Info();
		$language = $info->getLanguage('admin');
		
		if(empty($language)){
			$language = 'vi';
		}
		$translate = array(
							'adapter' => 'tmx',
							'content' => LANG_PATH . '/' . $language . '/admin/default.language.tmx',
							'locale' => $language,
							);
		$translate = new Zend_Translate($translate);
		Zend_Registry::set('Zend_Translate', $translate);
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;		
		//Truyen ra layout
		Zend_Layout::getMvcInstance()->assign('bg', 'bag-image-page');
		Zend_Layout::getMvcInstance()->assign('current', 'web');
		
		$template_path = TEMPLATE_PATH . "/front/system1";
		$this->loadTemplate($template_path,'template.ini','template');		
	}	
	public function indexAction(){
			
//		$tblProduct = new Zendme_Models_ProItem();
//		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-index'));	
//		$this->view->total = $tblProduct->countItem($this->_arrParam,array('task'=>'public-index'));		
//		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-index'));
//		$paginator = new Zendme_Paginator();
//		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
//		$home = new Zendme_Models_Home();
//		$homelist = $home->listItem($this->_arrParam,array('task'=>'admin-list'));
//		$title 			= $homelist[0]['title'];
//		$description 	= $homelist[0]['description_html'];
//		$keyword 		= $homelist[0]['keywords_html'];
//		$this->view->headTitle($title,true);
//		$this->view->headMeta(true)->setName('description',$description);
//		$this->view->headMeta(true)->setName('keywords',$keyword);

		$mdlProduct=new Zendme_Models_Products();               
        $items=$mdlProduct->listItem($this->_arrParam,array('task'=>'public-index'));
		$this->view->items=$items;
        
        $totalItem  = $mdlProduct->countItem($this->_arrParam,array('task'=>'public-index'));	
		$paginator = new Zendme_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);
		
		$this->view->headTitle('Thiết kế website');
		$this->view->headMeta()->appendName('description', ''); 
		$this->view->headMeta()->appendName('keywords', ''); 
	}	
	public function filterAction(){
		$ssFilter = new Zend_Session_Namespace($this->_namespace);
		if($this->_arrParam['type'] == 'search'){			
			if($this->_arrParam['key'] == 1){
				$ssFilter->keySearch = trim($this->_arrParam['keySearch']);
			}else{
				$ssFilter->keySearch = '';
			}
		}		
		$this->_redirect($this->_actionMain);
		$this->_helper->viewRenderer->setNoRender();
	}
	public function categoryAction(){
//		$tblProduct = new Zendme_Models_ProItem();
//		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'public-category'));
//		$tblCategoryMenu = new Zendme_Models_CatItem();
//		$this->view->tblCategoryMenu = $tblCategoryMenu->getItem($this->_arrParam,null);
//		$this->view->tblParentMenu = $tblCategoryMenu->getParent($this->_arrParam,null);
//		$this->view->Title = 'Products - ' . $this->view->Items[0]['category_name'];
//		$this->view->headTitle($this->view->Title,true);
//		$this->view->total = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));		
//		$totalItem  = $tblProduct->countItem($this->_arrParam,array('task'=>'public-category'));
//		$paginator = new Zendme_Paginator();
//		$this->view->panigator = $paginator->createPaginator($totalItem,$this->_paginator);

		$mdlCatalog=new Zendme_Models_Category();
		$category=$mdlCatalog->getItem($this->_arrParam,array('task'=>'public'));
		$this->view->category=$category;
			
		$mdlProduct =new Zendme_Models_Products();
		$items=$mdlProduct->listItem($this->_arrParam,array('task'=>'public-category'));
		$this->view->items=$items;
		
		$this->view->headTitle($category['name']);
		if(!empty($category['meta_description']))
		$this->view->headMeta()->appendName('description', $category['meta_description']); 
		if(!empty($category['meta_keyword']))
		$this->view->headMeta()->appendName('keywords', $category['meta_keyword']); 
	}
	
	public function detailAction(){		
//		$tblDetail = new Zendme_Models_ProItem();
//		$this->view->Item = $tblDetail->getItem($this->_arrParam,array('task'=>'public-detail'));
//		$this->view->Title = $this->view->Item['name'];
//		$this->view->headMeta()->appendName('description',$this->view->Item['name']);
//		$this->view->headTitle($this->view->Title,true);
		
		$mdlService=new Zendme_Models_Products();
		$item=$mdlService->getItem($this->_arrParam,array('task'=>'public-detail'));
		$this->view->item=$item;
		
		$this->view->headTitle($item['name']);
		if(!empty($item['meta_description']))
		$this->view->headMeta()->appendName('description', $item['meta_description']); 
		if(!empty($item['meta_keyword']))
		$this->view->headMeta()->appendName('keywords', $item['meta_keyword']); 		
	}
	public function viewCartAction(){
		$yourCart = new Zend_Session_Namespace('cart');		
		if($this->_request->isPost()){
			$itemProduct = $this->_arrParam['itemProduct'];			
			if(count($itemProduct)>0)
				foreach($itemProduct as $key => $val){
					if($val == 0 ){
						unset($itemProduct[$key]);
				}
			}			
			$yourCart->cart = $itemProduct;
		}		
		$ssInfo = $yourCart->getIterator();			
		$tblProduct = new Zendme_Models_ProItem;
		$this->_arrParam['cart'] = $ssInfo['cart'];		
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];
	}
	public function addItemAction(){				
		$yourCart = new Zend_Session_Namespace('cart');		
		$ssInfo = $yourCart->getIterator();		
		$filter = new Zend_Filter_Digits();
		$id = $filter->filter($this->_arrParam['id']);			
		if(count($yourCart->cart) == 0){			
			$cart[$id] = 1;
			$yourCart->cart = $cart;
		}else{
			$tmp = $ssInfo['cart'];
			if(array_key_exists($id,$tmp) == true){
				$tmp[$id] = $tmp[$id] + 1;
			}else{
				$tmp[$id] = 1;
			}			
			$yourCart->cart = $tmp;			
		}		
		$this->_helper->viewRenderer->setNoRender();
		$this->_redirect($this->_currentController . '/view-cart');
	}
	
	public function orderAction(){
		$yourCart = new Zend_Session_Namespace('cart');			
		$ssInfo = $yourCart->getIterator();		
		$tblProduct = new Zendme_Models_ProItem;
		$this->_arrParam['cart'] = $ssInfo['cart'];		
		$this->view->Items = $tblProduct->listItem($this->_arrParam,array('task'=>'view-cart'));
		$this->view->cart =  $ssInfo['cart'];		
		if($this->_request->isPost()){
			$validator = new Products_Form_ValidateInvoice($this->_arrParam);
			if($validator->isError() == true){
				$this->view->errors = $validator->getMessageError();
				$this->view->Item	= $validator->getData();
			}else{
				if(count($this->view->cart) > 0){
					$tblInvoice = new Zendme_Models_Invoice();
					$invoice_id = $tblInvoice->saveItem($this->_arrParam,array('task'=>'public-order'));			
					$tblInvoiceDetail = new Zendme_Models_InvoiceDetail();
					$this->_arrParam['invoice_id'] = $invoice_id;
					$tblInvoiceDetail->saveItem($this->_arrParam);
					$yourCart->unsetAll();
					$this->_redirect($this->_currentController . '/note');
				}else{
					$this->_redirect($this->_currentController . '/error-cart');
				}
			}
		}
	}
}